package com.lc.hongtai.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.lc.hongtai.MyApplication;
import com.lc.hongtai.R;
import com.lc.hongtai.conn.JsonAccessToken;
import com.lc.hongtai.conn.JsonBorrowdo;
import com.lc.hongtai.toast.LovelyToast;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;

;

public class IoanActivity extends BaseActivity implements View.OnClickListener {

    private Button bt02;

    private TextView et01;
    private EditText et02;

    //传值
    private String uid;
    private String borrow_type;
    private String money;
    private String sex;
    //最高投资奖励
    // private String zuigao_id;

    private String daishou;
    //是否按天
    private String antian_id;

    //贷款期限
    private String daikuanqixian;
    //借款用途
    private String borrow_use_id;
    //最低投资金额
    private String borrow_min_id;
    //最高投资金额
    private String zuigao_id;
    //有效时间
    private String borrow_time_id;
    //还款方式
    private String type_repyment;
    //是否有奖励
    private String no_yeid;
    //是否定向投标
    private String dingxiang_id;
    //定向投标的密码
    private String edit_pwd;

    public String token;

    public String borrow_payback_source_et;

    String edit_money;

    private EditText et_01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.INSTANCE.addActivity(this);
        setContentView(R.layout.activity_ioan);


        BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) getWindow().getDecorView()));
        uid = getIntent().getStringExtra("uid");
        borrow_type = getIntent().getStringExtra("borrow_type");
        money = getIntent().getStringExtra("text_user");
        sex = getIntent().getStringExtra("text_sex");


        //Log.d("adslkjfffffffffff", uid + borrow_type + money + sex + zuigao_id);
//待收金额限制
        daishou = getIntent().getStringExtra("daishou_id");
        antian_id = getIntent().getStringExtra("antian_id");
        daikuanqixian = getIntent().getStringExtra("daikuanqixian");
        borrow_use_id = getIntent().getStringExtra("borrow_use_id");
        //最低和最高投资金额
        borrow_min_id = getIntent().getStringExtra("borrow_min_id");
        zuigao_id = getIntent().getStringExtra("zuigao_id");
//有效时间
        borrow_time_id = getIntent().getStringExtra("borrow_time_id");
//还款方式
        type_repyment = getIntent().getStringExtra("type_repyment");

        no_yeid = getIntent().getStringExtra("no_yeid");

        //是否定向投标
        dingxiang_id = getIntent().getStringExtra("dingxiang_id");
        edit_pwd = getIntent().getStringExtra("edit_pwd");
        borrow_payback_source_et=getIntent().getStringExtra("borrow_payback_source_et");

        edit_money=getIntent().getStringExtra("edit_money");

        initView();
    }

    private void initView() {

        this.bt02 = (Button) findViewById(R.id.bt_02);
        this.et01 = (TextView) findViewById(R.id.et_01);
        this.et02 = (EditText) findViewById(R.id.et_02);
        bt02.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.bt_02:
                if (et01.getText().toString().trim().isEmpty() | et02.getText().toString().trim().isEmpty()) {
                    //  Toast.makeText(IoanActivity.this, "请填写完信息", Toast.LENGTH_SHORT).show();
                    LovelyToast.makeText(IoanActivity.this, "安全验证失败！", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else {
                    getAccessToken();
                }


                break;
        }

    }

    private void getAccessToken() {
        new JsonAccessToken("index/borrow_do", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                if (info != null) {
                    if (!TextUtils.isEmpty(info.access_token)) {
                        initData(info.access_token);
                    } else {
                       // UtilToast.show(IoanActivity.this, "安全验证失败！");
                        LovelyToast.makeText(IoanActivity.this, "安全验证失败", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
              //  UtilToast.show(IoanActivity.this, "安全验证失败！");
                LovelyToast.makeText(IoanActivity.this, "安全验证失败", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }
        }).execute(this, true);

    }


    private void initData(String access_token) {
//uid 用户id borrow_type 借款类型  money：借款金额 sex "daishou:限制待收"  et01:借款标题  antian_id：是否按天 贷款期限：daikuanqixian
        //借款用途 borrow_use_id   最低投资金额：borrow_min_id   最高 投资金额： zuigao_id
        //还款方式type_repyment
        // 有效时间：borrow_time_id    是否奖励  dingxiang_id:是否定向投标
        //定向投标的密码：edit_pwd
        Log.d(access_token + uid + borrow_type + money + sex + daishou + et01.getText().toString() + antian_id + daikuanqixian + "" + borrow_min_id + zuigao_id + borrow_min_id + type_repyment + no_yeid + no_yeid + dingxiang_id + edit_pwd + "说明", "1111111111111111111111");
//        daishou
        /*new JsonBorrowdo(access_token, uid, borrow_type,
                money, sex, edit_money, et02.getText().toString().trim(),
                antian_id, daikuanqixian, borrow_use_id, borrow_min_id, borrow_payback_source_et,
                zuigao_id, borrow_time_id, type_repyment, no_yeid, no_yeid,
                dingxiang_id, edit_pwd, et02.getText().toString().trim(),
                new AsyCallBack() {*/



        new JsonBorrowdo(access_token, uid, borrow_type,
                money, sex, edit_money,et01.getText().toString().trim(), et02.getText().toString().trim(),
                antian_id, daikuanqixian, borrow_use_id, borrow_min_id, borrow_payback_source_et,
                zuigao_id, borrow_time_id, type_repyment, no_yeid, no_yeid,
                dingxiang_id, edit_pwd, et02.getText().toString().trim(),
                new AsyCallBack() {

                    @Override
                    public void onSuccess(String toast, int type, Object o) throws Exception {
                        Log.d("accesstoken" + uid + borrow_type + money + sex + daishou + et01.getText().toString() + antian_id + daikuanqixian + "" + borrow_min_id + zuigao_id + borrow_min_id + type_repyment + no_yeid + no_yeid + dingxiang_id + edit_pwd + "说明", "1111111111111111111111");
                        if (o.equals("")) {
                            MyApplication.canClean = false;
                           /* ((MainActivity)).setSelect(1);
                            ((MainActivity)).setFirstSelect(false);
                            MyApplication.myPreferences.saveLoR(false);*/
                            finish();
                        }
                    }

                    @Override
                    public void onEnd(String toast, int type) throws Exception {
                     //   UtilToast.show(IoanActivity.this, JsonBorrowdo.TOAST);
                        LovelyToast.makeText(IoanActivity.this, JsonBorrowdo.TOAST, 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }

                }).execute(this, true);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.INSTANCE.finishActivity(this);
    }

    public void onBack(View view) {
        MyApplication.canClean = false;
        onBackPressed();
    }


}
